package com.hanbang.chu_xing_gao_zhao.myview;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanbang.chu_xing_gao_zhao.utils.MyToast;
import com.hanbang.chu_xing_gao_zhao.utils.TimeTools;
import com.hanbang.cxgz_2.R;

import java.text.ParseException;

/**
 * 日期时间选择控件
 */
public class CalendarPickerUtil implements OnDateChangedListener {
    /***
     * 日期控件
     */
    private DatePicker datePicker;
    /**
     * 对话框
     */
    private AlertDialog ad;
    private Activity activity;

    private TextView title;
    private TextView cancel;
    private TextView confirm;

    /**
     * 当前时间
     */
    private long crenterTime;
    /**
     * 最大日期值
     */
    private int maxTime;

    private OnClickCallback onClickCallback=null;

    /**
     * 弹出选择框构，并传入一个最大日期值
     */
    public CalendarPickerUtil(Activity activity, int maxTime) {
        this.activity = activity;
        this.maxTime = maxTime;
        crenterTime = TimeTools.getDateLong();
    }

    /**
     * 弹出日期时间选择框方法
     */
    public String dateTimePicKDialog() {
        LinearLayout dateTimeLayout = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.dialog_calender, null);
        title = (TextView) dateTimeLayout.findViewById(R.id.shoping_cart_dialog_title);
        cancel = (TextView) dateTimeLayout.findViewById(R.id.shoping_cart_dialog_cancel);
        confirm = (TextView) dateTimeLayout.findViewById(R.id.shoping_cart_dialog_confirm);
        title.setText(TimeTools.getYear() + "年" + TimeTools.getMonth() + "月" + TimeTools.getDay() + "日 ");
        datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);

        // dislog 的初始化
        ad = new AlertDialog.Builder(activity).setIcon(null).setTitle(null).setView(dateTimeLayout).show();

        /**
         * 取消键的监听
         */
        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ad.dismiss();

            }
        });

        /**
         * 确认键的监听
         */
        confirm.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    // 如果时间是当天，直接赋值返回
                    if (TimeTools.converLongToString(crenterTime, "yyyy-MM-dd").equals(TimeTools.getCalender())) {
//						inputDate.setText(title.getText().toString());


                        ad.dismiss();

                        return;
                    }
                    // 如果时间小于当天提醒用户
                    if (crenterTime + 1000 * 60 < TimeTools.getDateLong()) {
                        MyToast.show(activity, "您选择的日期不能小于今天，请重新选择", 1);

                        return;
                    }
                    // 如果时间大于10天之后提醒用户
//                    if (crenterTime > TimeTools.getDateLong() + 1000 * 60 * 60 * 24 * maxTime) {
//                        MyToast.show(activity, "您选择的日期不能大于10天，请重新选择", 1);
//                        return;
//                    }

                } catch (ParseException e1) {

                    e1.printStackTrace();
                }




//				inputDate.setText(title.getText().toString());

                ad.dismiss();
            }
        });




        onDateChanged(null, 0, 0, 0);

        ad.show();

        return title.getText().toString();
    }

    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

        // 改变日期的案值
        datePicker.init(TimeTools.getYear(), TimeTools.getMonth() - 1, TimeTools.getDay(), new OnDateChangedListener() {
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // 即时变dialog title
                title.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");

                try {
                    crenterTime = TimeTools.converStringToDateLong((year + "-" + (monthOfYear + 1) + "-" + dayOfMonth + "-"), "yyyy-MM-dd");

                    if (onClickCallback != null ) {
                        onClickCallback.onclick(title.getText().toString());

                    }

                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

        });

    }


    public interface OnClickCallback {
        public void onclick(String content);
    }

    public void setOnClickCallback(OnClickCallback callback) {
        onClickCallback = callback;

    }


}